<template>
  <!--临时工艺更改周转列表-->
  <div class="batch_list" v-show="showStatus">
    <div class="table_container">
      <el-table
        :data="listData.list"
        border
        stripe
        style="width: 100%"
        :max-height="tableCascadeHeight"
        ref="mesTable"
        v-loading="isLoading"
        @sort-change="_sort"
        @filter-change="handleFilterChange">
        <mes-col-comp
          :key="column.prop"
          v-for="column in tableColConfig"
          :column="column">
        </mes-col-comp>
        <el-table-column
          prop="id"
          label="操作"
          fixed="right"
          :resizable="false"
          align="center"
          min-width="160"
          show-overflow-tooltip>
          <template slot-scope="scope">
            <span
              class="jump_item"
              :class="{is_disabled: prohibit(scope.row)}"
              @click="editProcess(scope.row)">
              编辑工艺
            </span>
            <span class="jump_item" @click="showLog(scope.row)">操作记录</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <mes-pagination
      @updateData="init"
      :currentPage="listData.pageNo"
      :total="listData.count || 0"
      :pageSize="listData.pageSize">
    </mes-pagination>
    <!-- 日志详情 -->
    <log-detail ref="logDetail"></log-detail>
  </div>
</template>

<script>
import {TurnoverStatus, TaskType, TurnoverProgressStatus} from '@/components/mesDict'
import batchListCol from './batchListCol'

export default {
  name: 'BatchList',
  data() {
    return {
      isLoading: false, // 是否加载完毕
      showStatus: false, // 组件是否显示
      workorderIds: '', // 工单ID
      searchData: {}, // 筛选对象
      sortData: {}, // 排序对象
    }
  },
  components: {
    TurnoverStatus,
    TaskType,
    TurnoverProgressStatus
  },
  computed: {
    tableColConfig: batchListCol,
    partBatchProcessFilter() {
      let batchProcessStatus = this.$store.state.dict.batchProcessStatus.labels || []
      return batchProcessStatus.filter((item) => {
        return item.value !== 'InTechProc'
      })
    },
    listData() {
      return this.$store.state.temporaryWorkbench.batchList || {}
    }
  },
  methods: {
    // 排序
    _sort(obj) {
      this.sortData = this.$getSortOrder(obj)
      this.init()
    },
    // 列筛选回调
    handleFilterChange(filters) {
      let query = this.$filterParams(this.tableColConfig, filters)
      Object.assign(this.searchData, query.search)
      this.$refs.mesTable.clearSort()
      this.sortData = query.order
      this.init()
    },
    // 请求数据整理
    getPostData(page) {
      const fields = ['workorderId', 'workorderNumber', 'id', 'turnoverStatusId', 'turnoverStatusValue', 'turnoverProcessStatusId', 'turnoverProcessStatusValue', 'trackDemandId', 'trackDemandValue', 'taskTypeId', 'taskTypeValue']
      let postData = {
        pageNo: page ? page.pageNo : 1,
        pageSize: page ? page.pageSize : this.listData.pageSize || 15,
        sort: this.sortData,
        query: {
          columns: this.tableColConfig.reduce((pre, cur) => [...pre, cur.prop], fields),
          ...this.searchData,
          viewSource: 8,
        }
      }
      postData.query.workorderId = this.workorderIds
      if (!postData.query.turnoverProcessStatusId) {
        postData.query.turnoverProcessStatusId = this.partBatchProcessFilter.reduce((pre, cur) => [...pre, cur.id], []).join(',')
      }
      return postData
    },
    /**
     * 列表数据请求
     * @param page
     */
    init(page) {
      this.isLoading = true
      let postData = this.getPostData(page)
      this.$store.dispatch('saveData', {
        method: 'post',
        url: 'a/turnoverbatch/turnoverBatch/list',
        data: postData,
        module: 'temporaryWorkbench',
        state: 'batchList'
      }).finally(() => {
        this.isLoading = false
      })
    },
    /**
     * 获取记录id并请求已拆分的列表
     * @param ids 工单id
     */
    showBatchList(ids) {
      this.workorderIds = ids
      if (ids) {
        this.showStatus = true
        this.init()
      } else {
        this.showStatus = false
        this.$store.commit('SetState', {
          module: 'temporaryWorkbench',
          state: 'batchList',
          data: {
            pageSize: this.listData.pageSize,
          }
        })
      }
    },
    // 禁止工艺编辑判断
    prohibit(row) {
      if (row.turnoverProcessStatusValue === 'Finished') {
        // 周转进度为已完工
        return true
      } else if (row.turnoverStatusValue !== 'Normal' && row.turnoverStatusValue !== 'Paused') {
        // 周转状态不是正常也不是已暂停
        return true
      } else if (row.turnoverStatusValue === 'Normal' && row.turnoverProcessStatusValue === 'Producing') {
        // 周转状态为正常，周转进度为生产中
        return true
      }
      return false
    },
    editProcess(row) {
      // 周转状态不是正常或已暂停，或周转进度是已完工的，不可以进行工艺编制
      if (this.prohibit(row)) return
      this.$router.push({
        path: '/tempWorkStageEdit',
        query: {
          id: row.id
        }
      })
    },
    // 日志
    showLog(row) {
      this.$refs.logDetail.openDialog({
        title: '周转码',
        name: row.number,
        url: 'a/turnoverstatuschangehistory/turnoverStatusChangeHistory/list',
        query: {
          turnoverBatchId: row.id
        }
      })
    },
  }
}
</script>

<style scoped lang="less">
  .batch_list{
    .btn_cell{
      color: #06c;
      cursor: pointer;
    }
  }
</style>
